﻿using MediatR;
using W.Purchase.API.Application.Command.SaleCommand;
using W.Purchase.Domain.DTO;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.SaleCommandHandler
{
    public class SaleOrderFanCommandHandler : IRequestHandler<SaleOrderFanCommand, List<SalesDTO>>
    {
        private readonly ISalesOrdersRepository salesOrdersRepository;

        private readonly IProductsSalesRepository productsSalesRepository;

        public SaleOrderFanCommandHandler(ISalesOrdersRepository salesOrdersRepository, IProductsSalesRepository productsSalesRepository)
        {
            this.salesOrdersRepository = salesOrdersRepository;
            this.productsSalesRepository = productsSalesRepository;
        }

        public async Task<List<SalesDTO>> Handle(SaleOrderFanCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await salesOrdersRepository.GetAllAsaync()
                       join b in await productsSalesRepository.GetAllAsaync() on a.SalesOrdersId equals b.SalesOrdersId
                       where a.SalesOrdersId == request.SalesOrdersId
                       select new SalesDTO
                       {
                           SalesOrdersId = a.SalesOrdersId,
                           SalesOrdersNo = a.SalesOrdersNo,
                           ProcurementTypeId = a.ProcurementTypeId,
                           AssociateNumber = a.AssociateNumber,
                           ClientNo = a.ClientNo,
                           ClientName = a.ClientName,
                           SalesOrdersPerson = a.SalesOrdersPerson,
                           SalesOrdersPhone = a.SalesOrdersPhone,
                           ShipmentsDate = a.ShipmentsDate,
                           SalesOrdersRemark = a.SalesOrdersRemark,
                           SingleMaker = a.SingleMaker,
                           SingleTime = a.SingleTime,
                           OrderStatus = a.OrderStatus,
                           OrderSale = a.OrderSale,
                           Recorded = a.Recorded,
                           office_id = a.office_id,
                           user_id = a.user_id,
                           ProductsSalesId = b.ProductsSalesId,
                           ProductName = b.ProductName,
                           ProductNo = b.ProductNo,
                           ProductSpec = b.ProductSpec,
                           UnitName = b.UnitName,
                           Price = b.Price,
                           ProductsSalesNum = b.ProductsSalesNum,
                           ProductsSalesTotal = b.ProductsSalesTotal,
                           SaleReturnsNum = b.SaleReturnsNum
                       };
            return list.ToList();
        }
    }
}
